<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/7/14
 * Time: 11:04 AM
 */

namespace Admin\Model;
use stdClass;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Exception;


class AdminAppTable
{
    protected $tableGateway;


    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }




    public function fetchAll()
    {
        $select = new Select('appversion');
        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new AdminApp());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
        // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }




    public function getApp($id)
    {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            return null;
        }
        return $row;
    }

    public function getLastVersion(){

        $str = " SELECT * FROM appversion ORDER BY id limit 0,1 ";
        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute($str);

        $statement = $result->getResource();
        $resultSet1 = $statement->fetchAll(\Pdo::FETCH_NAMED);
        // $resultSet1 = $statement->fetchAll(\Pdo::FETCH_OBJ);
        return $resultSet1;
    }


    public function getLastIdInsert(){
        return $this->tableGateway->lastInsertValue;
    }





    public function saveApp(AdminApp $app)
    {
        $data = array(
            'name' => $app->name,
            'version'  => $app->version,
        );

        $id = (int)$app->id;
        if ($id == 0) {
            if ($this->tableGateway->insert($data)) return $this->tableGateway->getLastInsertValue();
            else return false;
        } else {
            if ($this->getApp($id)) {
                if ($this->tableGateway->update($data, array('id' => $id))) return true;
                else return false;
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }


    public function save($data = array(),$where = array()){
      return  $this->tableGateway->update($data, $where);
    }




    public function deleteApp($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }



    public function getAppWhere($where = array()){
        $where_sql = '';
        $i = 0;
        foreach($where as $key => $value){
            $i++;
            if($i==1){
                $where_sql .= " `".$key."` = '".$value."' " ;
            }
            if($i>1){

                $where_sql .= " AND `".$key."` =  '".$value."' ";

            }
        }
        $str = " SELECT * FROM appversion WHERE ".$where_sql;
        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute($str);

        $statement = $result->getResource();
        $resultSet1 = $statement->fetchAll(\Pdo::FETCH_NAMED);
        // $resultSet1 = $statement->fetchAll(\Pdo::FETCH_OBJ);
        return $resultSet1;
    }
}